package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorAdvertisingPush;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.OperatorAdvertisingPushRepository;

@Service
public class OperatorAdvertisingPushService implements OperatorAdvertisingPushServiceInterface {
	@Resource
	private OperatorAdvertisingPushRepository advertisingPushRepository;

	@Override
	public ReturnResult saveOrUpdate(String pushSwitch, String pushConditions, String pushUrl, String pushImage, String cityId) {
		OperatorAdvertisingPush advertisingPush = null;
		List<OperatorAdvertisingPush> advertisingPushs = advertisingPushRepository.findByCityId(cityId);
		if (advertisingPushs != null && advertisingPushs.size() >0) {
			advertisingPush = advertisingPushs.get(0);
		} else {
			advertisingPush = new OperatorAdvertisingPush();
			
		}
		advertisingPush.setPushSwitch(pushSwitch);
		advertisingPush.setPushUrl(pushUrl);
		advertisingPush.setPushConditions(pushConditions);
		advertisingPush.setPushImage(pushImage);
		advertisingPush.setCityId(cityId);
		advertisingPush.setCreateTime(new Date());
		ReturnResult returnResult = new ReturnResult();
		if (advertisingPush != null) {
			advertisingPush.setCreateTime(new Date());
			OperatorAdvertisingPush advertising = advertisingPushRepository.save(advertisingPush);
			if (advertising != null) {
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
				return returnResult;
			}
			
		}
		returnResult.setKey("error");
		returnResult.setValue("保存失败");
		return returnResult;
	}

	@Override
	public ReturnResult getAdvertisingPuth(String cityId) {
		ReturnResult returnResult = new ReturnResult();
		List<OperatorAdvertisingPush> advertisingPushs = advertisingPushRepository.findByCityIdAndPushSwitch(cityId, "1");
		if (advertisingPushs != null && advertisingPushs.size() >0) {
			returnResult.setKey("success");
			returnResult.setValue(advertisingPushs.get(0));
		} else {
			returnResult.setKey("error");
			returnResult.setValue(null);
		}
		return returnResult;
	}

}
